/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schtohan.managedbeans;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import schtohan.business.DbController;
import schtohan.entities.Journey;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Schtohan
 */
@ManagedBean
@SessionScoped
public class searchBean {

    /**
     * Creates a new instance of searchBean
     */
    public searchBean() {
    }
    
    private String startDate;
    private List<Journey> journeys = new ArrayList<Journey>();

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public List<Journey> getJourneys() {
        return journeys;
    }

    public void setDepartures(ArrayList<Journey> departures) {
        this.journeys = departures;
    }
    
    public String findJourneys() {
        
        try {
            DbController db = new DbController();
            journeys = db.findJourneys(startDate);
            if(journeys.isEmpty()) {
                return "index";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            for(Journey j : journeys)
            {
                String departureTime = sdf.format(j.getStartTime());
                j.setStartTimeString(departureTime);
            }
            return "viewJourneys";
        }
        catch (Exception e){
            return "index";
        }
    }
}
